"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin插件
Case Name   : 频繁加载插件，是否正常
Create At   : 2022.04.14
Owner       : z@wan005
Description :
    1.创建兼容B库100次
    2.连接b类型数据库查询插件
    3.测试兼容函数
    4.删除数据库
    5.切回A类型的数据库，执行regex函数
    6.清理环境
Expect      :
    1.成功
    2.自动加载成功
    3.成功
    4.成功
    5.报错
    7.无须清理环境
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Plugin034(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.db_name = "db_plugin0034"

    def test_dolphin(self):

        text = '--step1:创建兼容B库100次并查询插件是否存在;expect:成功---'
        self.log.info(text)
        for i in range(100):
            sql_cmd = f"drop database if exists  {self.db_name}_{i};" \
                      f"create database {self.db_name}_{i} " \
                      f"DBCOMPATIBILITY ='B';"
            msg = self.sh_primy.execut_db_sql(sql_cmd)
            self.log.info(msg)
            self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg,
                          '执行失败' + text)

            text = '--step2:连接b类型数据库查询插件;expect:自动加载成功---'
            self.log.info(text)
            sql_cmd = f"select extname from pg_extension where extname " \
                      f"='dolphin';"
            msg = self.sh_primy.execut_db_sql(sql_cmd,
                                              dbname=f'{self.db_name}_{i}')
            self.log.info(msg)
            self.assertIn('dolphin', msg, '执行失败' + text)

            text = '--step3:测试兼容函数;expect:成功---'
            self.log.info(text)
            cmd = "select regexp('a', true);"
            msg = self.sh_primy.execut_db_sql(cmd,
                                              dbname=f'{self.db_name}_{i}')
            self.log.info(msg)
            self.assertEqual('1', msg.splitlines()[2].strip(),
                             '执行失败' + text)

            text = '--step4:删除数据库;expect:成功--'
            self.log.info(text)
            sql_cmd = f'''drop database if exists  {self.db_name}_{i};'''
            msg = self.sh_primy.execut_db_sql(sql_cmd)
            self.log.info(msg)
            self.assertIn(self.constant.DROP_DATABASE_SUCCESS, msg,
                          '执行失败' + text)

            text = '--step5:切回A类型的数据库，执行regex函数;expect:报错--'
            self.log.info(text)
            cmd = "select regexp('a', true);"
            msg = self.sh_primy.execut_db_sql(cmd,
                                              dbname='postgres')
            self.log.info(msg)
            self.assertIn('ERROR:  function regexp', msg, '执行失败' + text)

    def tearDown(self):
        text = '--step6:清理环境;expect:无须清理环境完成---'
        self.log.info(text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
